<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/27
 * Time: 14:21
 */
namespace app\web\controller;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalCategoryPostModel;
use app\portal\model\PortalPostModel;
use app\portal\model\AssetModel;
use app\portal\service\PostService;
use cmf\controller\HomeBaseController;
class InformationController extends HomeBaseController
{
    /**
     * 首页信息列表
     */
    public function getHomepageInfo()
    {
        $param = $this->request->param();
        $type = $this->request->param('type', 0, 'intval');
        $result = $this->validate($param, 'Information');
        $portalCategoryModel = new PortalCategoryModel();
        $portalCategoryPostModel = new PortalCategoryPostModel();
        $portalPostModel = new PortalPostModel();
        if ($result !== true) {
            return $this->json('1003', $result);
        } else {
            $portalCategory = $portalCategoryModel->getPortalCategory($type);
            $ids = array_column($portalCategory, 'id');
            $portalCategoryPost = $portalCategoryPostModel->getPortalCategoryPostId($ids);
            if($type == 3){
                $limit = 6;
            }else{
                $limit = 3;
            }
            if ($param['languageType'] == 1){
                $condition['link_id'] = 0;
            }else{
                $condition['link_id'] = array('neq',0);
            }
            $condition['id'] = array('in',$portalCategoryPost);
            $condition['post_status'] = 1;
//            $condition['recommended'] = 1;
            $portalPostList = $portalPostModel->where($condition)->order('published_time','desc')->limit($limit)->select();
            $portalPostListNew = array();
            foreach ($portalPostList as $k => $value){
                if ($param['languageType'] == 1){
                    $portalPostListNew[$k]['portalPostId'] = $value['id'];
                    $portalPostListNew[$k]['portalPostEnglishId'] = $value['f_link_id'];
                }else{
                    $portalPostListNew[$k]['portalPostId'] = $value['link_id'];
                    $portalPostListNew[$k]['portalPostEnglishId'] = $value['id'];
                }
                $portalPostListNew[$k]['thumbnail'] = cmf_get_image_preview_url($value['thumbnail']);
                $portalPostListNew[$k]['title'] = $value['post_title'];
                $portalPostListNew[$k]['excerpt'] = $value['post_excerpt'];
                $portalPostListNew[$k]['publishedTime'] = date('Y/m/d',$value['published_time']);
            }
            if (empty($portalPostListNew)){
                return $this->json('1007', '无数据');
            }
            return $this->json('0', '请求成功', $portalPostListNew);
        }
    }
    /**
     * 获取分类名称
     */
    public function getCategoryName(){
        $param = $this->request->param();
        $portalCategoryModel = new PortalCategoryModel();
        $portalCategory = $portalCategoryModel->getPortalCategory($param['type']);
        $data = array();
        foreach ($portalCategory as $k => $val){
            $data[$k]['categoryId'] = $val['id'];
            if ($param['languageType'] == 1){
                $data[$k]['name'] = $val['name'];
            }else{
                $data[$k]['name'] = $val['alias'];
            }
        }
        array_shift($data);
        return $this->json('0', '请求成功',  $data);
    }
    /**
     * 获取列表
     */
    public function getList()
    {
        $param = $this->request->param();
        $pageNo = $this->request->param('pageNo', 0, 'intval');
        $pageSize = $this->request->param('pageSize', 0, 'intval');
        $result = $this->validate($param, 'Information');
        $portalCategoryModel = new PortalCategoryModel();
        $portalCategoryPostModel = new PortalCategoryPostModel();
        $portalPostModel = new PortalPostModel();
        if ($result !== true) {
            return $this->json('1003', $result);
        } else {
            if (empty($param['childCategoryId'])){
                $portalCategory = $portalCategoryModel->getPortalCategory($param['type']);
                $ids = array_column($portalCategory, 'id');
            }else{
                $ids = $param['childCategoryId'];
            }
            $portalCategoryPost = $portalCategoryPostModel->getPortalCategoryPostId($ids);
            $condition['id'] = array('in',$portalCategoryPost);
            if ($param['languageType'] == 1){
                $condition['link_id'] = 0;
            }else{
                $condition['link_id'] = array('neq',0);
            }
            $condition['post_status'] = 1;
            $portalPostList = $portalPostModel->where($condition)->select();
            $count = count($portalPostList);
            $portalPostList = $portalPostModel->where($condition)->order('published_time','desc')->limit($pageSize*($pageNo-1),$pageSize)->select();
            $portalPostListNew = array();
            foreach ($portalPostList as $k => $value){
                if ($param['languageType'] == 1){
                    $portalPostListNew[$k]['portalPostId'] = $value['id'];
                    $portalPostListNew[$k]['portalPostEnglishId'] = $value['f_link_id'];
                }else{
                    $portalPostListNew[$k]['portalPostId'] = $value['link_id'];
                    $portalPostListNew[$k]['portalPostEnglishId'] = $value['id'];
                }
                $portalPostListNew[$k]['thumbnail'] = cmf_get_image_preview_url($value['thumbnail']);
                $portalPostListNew[$k]['title'] = $value['post_title'];
                $portalPostListNew[$k]['excerpt'] = $value['post_excerpt'];
                $portalPostListNew[$k]['publishedTime'] = date('Y-m-d',$value['published_time']);
            }
            if (empty($portalPostListNew)){
                return $this->json('1007', '无数据');
            }
            $total_page = ceil($count/$pageSize);
            $page_info = $this->setPageInfo($count,$total_page,$pageNo,$pageSize);
            return $this->json('0', '请求成功', $portalPostListNew,$page_info);
        }
    }


    /**
     * 获取文章详情
     */
    public function getArticleInfo()
    {
        $param = $this->request->param();
        if ($param['languageType'] == 2){
            $condition['link_id'] = $param['portalPostId'];
        }else{
            $condition['id'] = $param['portalPostId'];
        }
        $condition['post_status'] = 1;
        $portalPostModel = new PortalPostModel();
        $portalCategoryModel = new PortalCategoryModel();
        $portalCategoryPostModel = new PortalCategoryPostModel();
        $portalPostList = $portalPostModel->where($condition)->find();
        $portalCategory = $portalCategoryModel->getPortalCategory(3);
        $ids = array_column($portalCategory, 'id');
        $portalCategoryPost = $portalCategoryPostModel->getPortalCategoryPostId($ids);
        if (!empty($param['turnPiece'])){
            $portalPost = $portalPostModel->getFrontAfterArticleTitle($portalPostList,$portalCategoryPost,$param['languageType']);
            if ($param['turnPiece'] == 1){
                $portalPostList =  $portalPost[0];
            }else{
                $portalPostList =  $portalPost[1];
            }
        }
        if (empty($portalPostList)){
            return $this->json('1007', '无数据');
        }
        $portalCategoryPostInfo = $portalCategoryPostModel->where(['post_id' => $portalPostList['id']])->find();
        $portalCategoryInfo = $portalCategoryModel->where(['id' => $portalCategoryPostInfo['category_id']])->find();
        $portalPostListNew = array();
        $portalPostListNew['portalPostId'] = $portalPostList['id'];
        if ($param['languageType'] == 1){
            $portalPostListNew['portalPostId'] = $portalPostList['id'];
            $portalPostListNew['portalPostEnglishId'] = $portalPostList['f_link_id'];
            $portalPostListNew['categoryName'] = $portalCategoryInfo['name'];
        }else{
            $portalPostListNew['portalPostId'] = $portalPostList['link_id'];
            $portalPostListNew['portalPostEnglishId'] = $portalPostList['id'];
            $portalPostListNew['categoryName'] = $portalCategoryInfo['alias'];
        }
        $portalPostListNew['thumbnail'] = cmf_get_image_preview_url($portalPostList['thumbnail']);
        $portalPostListNew['title'] = $portalPostList['post_title'];
        $portalPostListNew['excerpt'] = $portalPostList['post_excerpt'];
        $portalPostListNew['content'] = $portalPostList['post_content'];
        $portalPostListNew['publishedTime'] = date('Y-m-d',$portalPostList['published_time']);
        if ($portalCategoryInfo['parent_id'] == 2){
            $turnArticle = $portalPostModel->getFrontAfterArticleTitle($portalPostList,$portalCategoryPost,$param['languageType']);
            if ($turnArticle[0]['post_title'] == ''){
                $portalPostListNew['beforeTitle'] = '';
            }else{
                $portalPostListNew['beforeTitle'] = $turnArticle[0]['post_title'];
            }
            if ($turnArticle[1]['post_title'] == ''){
                $portalPostListNew['afterTitle'] = '';
            }else{
                $portalPostListNew['afterTitle'] = $turnArticle[1]['post_title'];
            }
        }
        return $this->json('0', '请求成功', $portalPostListNew);
    }

    /**
     * 获取服务预约文件
     */
    public function getServiceReservation(){
        $AssetModel = new AssetModel();
        $Asset = $AssetModel->where(['filename' => '订 舱 委 托 书 .docx'])->find();
        $AssetNew['filePath'] = cmf_get_image_preview_url($Asset['file_path']);
        return $this->json('0', '请求成功', $AssetNew);
    }


}